<?php
/**
 * 《试卷题目》实现类
 *
 * @author 孙晓晔
 * @version $Id: Country.class.php 832 2010-01-06 09:17:14Z xuy $
 */

require_once(dirname(__FILE__) . '/ExamUtil.class.php');

class ExamEmp extends ExamUtil {
	public $tbl = Constant::tbl_exam_emp;

	public $status = array(
		'-' => '请选择',
		'1' => '等待答题',
		'2' => '正在答题',
		'3' => '等待阅卷',
		'4' => '正在阅卷',
		'5' => '完成阅卷',
		
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'exam' => '试卷',
		'emp' => '考试人员',		
		'status' => '状态',

		'1' => '创建时间',
		'2' => '更新时间',
	);
	
	public $groupby = array(
		'-' => '请选择',
		'mold' => '付款信息',
		'emp' => '工程师',
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}
	
	/**
	 * 首页
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function layout($controller, $request) {
		
		$data = array(
			'付款信息' => array('cc'=>'finance.php?do=Mold.find', 'js'=>''),
		    '付款明细' => array('cc'=>'finance.php?do=MoldLog.find', 'js'=>''),
		);
		
		$ret = array();
		while (list($k, $v) = each($data)) {
			// debug(print_r($v, true));
			if ($this->tree_my($v)) {
				$ret[$k] = $v; 
			}
		}

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'emp/layout');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-7 day'));
		
		$controller->display($request, 'exam/exam/emp/find');
		return true;
	}
	
	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == 'mobile') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['mobile'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$exam_s = $this->_input($request['exam_s']);
				
		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}

		if (!empty($exam_s)) {
			$where .= " AND exam = '" . $exam_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}

		if ($this->_audit('148')) {
			return '';
		}

		$emp = $_SESSION['id'];

		return " emp = '" . $emp . "'";
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {			
			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
				
				$row['exam_t'] = $this->exam_t($row['exam']);
				
				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'exam/exam/emp/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function my($controller, $request) {

		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);
				
		$this->_question($controller, $row['exam'], $row['emp']);		

		$controller->display($request, 'exam/exam/emp/edit');
	}

	function _question($controller, $exam, $emp) {
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT a.* FROM %s a, %s b WHERE a.id = b.question AND b.exam = '%s' AND b.status = '1' ORDER BY a.type";
		$sql = sprintf($fmt, Constant::tbl_question, Constant::tbl_exam_question, $exam);
		debug($sql);		
		$result = $db->query($sql);
		
		$type = 0;
		$dd = 1;
		$total = 0;
		$data = array();
		while ($row = $db->fetch_array($result)) {			
			$answer = $this->_answer_get($exam, $emp, $row['id']);
			$row['answer_x'] = $answer['answer'];
			$row['answer_o'] = $answer;
			
			if ($answer['answer'] == $row['answer']) {
				$row['score_x'] = $row['score'];
			} else {
				$row['score_x'] = $answer['score'];
			}
			
			if ($row['type'] < 4) {
				$a = preg_split("/[\n,]+/", trim($row['item']));
				
				$b = array();
				$count = count($a);
				for ($i = 0; $i < $count; $i++) {
					$ss = strval($i);
					if (strpos($answer['answer'], $ss) === false) {
						$b[] = array('v'=>$a[$i], 'c'=>'');;
					} else {
						$b[] = array('v'=>$a[$i], 'c'=>'checked');;
					}
				}
	
				$row['item_t'] = $b;	

				$row['answer'] = $this->_ab($row['answer']);
			}
			
			$total += $row['score'];
			
			if ($type != $row['type']) {
				$row['type_t'] = sprintf("%s%s", $this->_cd($dd), $this->type[$row['type']]);
				$type = $row['type'];
				$dd++;
			}
			
			$data[] = $row;
		}
		
		// debug(print_r($data, true));
		
		$controller->assign_by_ref('total', $total);
		$controller->assign_by_ref('data', $data);
	}
	
	function _answer_get($exam, $emp, $question) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE exam = '%s' AND emp = '%s' AND question = '%s'";
		$sql = sprintf($fmt, Constant::tbl_exam_answer, $exam, $emp, $question);
		// debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			debug(print_r($row, true));
			
			return $row;
		}
		
		return '';
	}

	function _answer_get222($exam, $emp, $question) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE exam = '%s' AND emp = '%s' AND question = '%s'";
		$sql = sprintf($fmt, Constant::tbl_exam_answer, $exam, $emp, $question);
		// debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			return $row['answer'];
		}
		
		return '';
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);
		
		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);

		$total = $request['total'];
		$score = $request['score'];
		$status = $request['status'];
		
		$percent = number_format(($score / $total) * 100, 2, '.', '');

		$db = newdb($this->dsn_w);
		
		if ($status == '5') {
			$fmt = "UPDATE %s SET status = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $status, $id);
			debug($sql);
			$db->query($sql);
		}
		
		$fmt = "UPDATE %s SET score = '%s', percent = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $score, $percent, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
		
		$score_a = $request['score_a'];
		while (list($k, $v) = each($score_a)) {			
			$this->_score($v['name'], $v['value']);	
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _score($id, $score) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET score = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, Constant::tbl_exam_answer, $score, $id);
		debug($sql);
		$db->query($sql);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);
		
		$controller->assign_by_ref('result', $row);

		$this->_question($controller, $row['exam'], $row['emp']);
		
		$controller->display($request, 'exam/exam/emp/view');
		return true;
	}

	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row = $this->_view_t($row);

		return $row;
	}
	
	function _list($id, $status = 'all') {
		$db = newdb($this->dsn_r);
		if ($status == 'all') {
			$fmt = "SELECT * FROM %s WHERE mold = '%s' ORDER BY id";
			$sql = sprintf($fmt, $this->tbl, $id);
		} else {
			$fmt = "SELECT * FROM %s WHERE mold = '%s' AND status = '%s' ORDER BY id";
			$sql = sprintf($fmt, $this->tbl, $id, $status);
		}
		
		// debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['emp_t'] = Name::emp($row['emp']);
			$row['status_t'] = $this->status[$row['status']];
			$row['ok'] = true;			

			$ret[] = $row;
		}

		return $ret;
	}
	
	function _insert($id, $log, $emp) {
		$db = newdb($this->dsn_w);		
		$fmt = "INSERT INTO %s(mold, `item`, total, amount, rate, rmb, date, content, emp, oper, createtime, modifytime) ";
		$fmt .= "VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		while (list($k, $v) = each($log)) {
			if (!$this->_ok($v['ok'])) {
				continue;
			}
			
			$sql = sprintf($fmt, $this->tbl, $id, $v['item'], $v['total'], $v['amount'], $v['rate'], $v['rmb'], $v['date'], $v['content'], $emp, $_SESSION['id']);
			debug($sql);
			$db->query($sql);
		}
	}
	
	function _update($exam, $list) {
		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET status = '2' WHERE exam = '%s'";
		$sql = sprintf($fmt, $this->tbl, $exam);
		debug($sql);
		$db->query($sql);
				
		$bb = explode(',', $list);
		while (list($k, $emp) = each($bb)) {
			$fmt = "SELECT * FROM %s WHERE emp = '%s' AND exam = '%s'";
			$sql = sprintf($fmt, $this->tbl, $emp, $exam);
			$result = $db->query($sql);
			if ($db->num_rows($result) > 0) {
				$fmt = "UPDATE %s SET status = '1', modifytime = now() WHERE emp = '%s' AND exam = '%s'";
				$sql = sprintf($fmt, $this->tbl, $emp, $exam);
			} else {
				$fmt = "INSERT INTO %s(emp, exam, oper, createtime, modifytime) VALUES('%s', '%s', '%s', now(), now())";
				$sql = sprintf($fmt, $this->tbl, $emp, $exam, $_SESSION['id']);
			}
			debug($sql);
			$db->query($sql);
		}
	}
	
	function _insert_my($id, $log, $emp) {
		$emp = $_SESSION['id'];
		
		$db = newdb($this->dsn_w);		
		$fmt = "INSERT INTO %s(mold, `item`, total, amount, content, emp, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		while (list($k, $v) = each($log)) {
			if (!$this->_ok($v['ok'])) {
				continue;
			}
			
			$sql = sprintf($fmt, $this->tbl, $id, $v['item'], $v['total'], $v['amount'], $v['content'], $emp, $emp);
			debug($sql);
			$db->query($sql);
		}
	}
	
	function _update_my($id, $log, $emp) {
		$emp = $_SESSION['id'];
		
		$db = newdb($this->dsn_w);		
		while (list($k, $v) = each($log)) {	
			if (array_key_exists("createtime", $v)) {
				if ($v['status'] > 1) {
					continue;
				}
					
				if (!$this->_ok($v['ok'])) {
					$fmt = "DELETE FROM %s WHERE id = '%s'";
					$sql = sprintf($fmt, $this->tbl, $v['id']);
				} else {									
					$fmt = "UPDATE %s SET item = '%s', total = '%s', amount = '%s', content = '%s', modifytime = now() WHERE id = '%s'";
					$sql = sprintf($fmt, $this->tbl, $v['item'], $v['total'], $v['amount'], $v['content'], $v['id']);
				}			
			} else {
				if (!$this->_ok($v['ok'])) {
					continue;
				}
				
				$fmt = "INSERT INTO %s(mold, `item`, total, amount, content, emp, oper, createtime, modifytime)";
				$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
				$sql = sprintf($fmt, $this->tbl, $id, $v['item'], $v['total'], $v['amount'], $v['content'], $emp, $_SESSION['id']);			
			}
			
			debug($sql);
			$db->query($sql);
		}
	}
	
	function finish($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$row['emp_t'] = Name::emp($row['emp']);

		$controller->assign_by_ref('result', $row);
		
		$data = $this->_mold($row['mold']);
		$data['exchange'] = $this->_exchange($data['currency']);
		
		$controller->assign_by_ref('mold', $data);

		$controller->display($request, 'finance/mold/log/finish');
		return true;
	}
	
	function finishPost($controller, $request) {
		$this->pretreat($controller);
		
		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);

		$amount = $request['amount'];
		$date = $request['date'];
		$rate = $request['rate'];
		$rmb = $request['rmb'];
		$content = addslashes($request['content']);
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET amount = '%s', date = '%s', rate = '%s', rmb = '%s', content = '%s', status = '3', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $amount, $date, $rate, $rmb, $content, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
		
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			
			$row = $this->id($this->tbl, $id);
			
			if ($this->_is_delete($row)) {
				$fmt = "DELETE FROM %s WHERE id = '%s'";
				$sql = sprintf($fmt, $this->tbl, $id);
				// debug($sql);
				$db->query($sql);
			}
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($row['status'] > 1) {
			return false;
		}

		if ($this->_audit('77')) {
			return true;
		}

		if ($row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}
	
	function group($controller, $request) {
		$this->pretreat_group($controller);

		// $controller->assign('t1', strtotime('-1 month'));
		// $controller->assign('t2', strtotime('+1 day'));

		$num = array(
			'1' => '按次数',
			'2' => '按金额',
		);
		$controller->assign('num_option', $num);

		$controller->display($request, 'finance/mold/log/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function groupby_dd($px) {
		if ($px == '1') {
			return "DATE_FORMAT(date, '%Y-%m-%d %H')";
		}

		if ($px == '2') {
			return "DATE_FORMAT(date, '%Y-%m-%d')";
		}

		if ($px == '3') {
			return "DATE_FORMAT(date, '%Y-%m')";
		}

		return $px;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);

		$num_s = $request['num_s'];
		if (!empty($num_s) && $num_s == '2') {
			$fmt = "SELECT %s AS a, SUM(total) AS c FROM %s a ";
		} else {
			$fmt = "SELECT %s AS a, COUNT(total) AS c FROM %s a ";
		}
		$sql = sprintf($fmt, $this->s, $this->tbl);

		$where = $this->_where($request);

		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);

		$this->_group($controller, $request);

		// $controller->display($request, 'list');
		return true;
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} else {

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}
	
	function start($controller, $request) {
		$this->pretreat($controller);
		
		$id = $request['id'];
		
		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);
		
		$ret = array('status'=>2);
		
		if ($row['status'] == '3') {
			$db = newdb($this->dsn_w);
			$fmt = "UPDATE %s SET status = '4' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $id);
			$db->query($sql);
			
			$ret['status'] = 1;
		}

		echo json_encode($ret);
		return true;
	}
}
?>